<?php

/**
 * This is the model class for table "question".
 *
 * The followings are the available columns in table 'question':
 * @property integer $id
 * @property string $title
 * @property string $type
 * @property integer $is_mark
 * @property integer $created_date
 * @property integer $created_by
 */
class Question extends CActiveRecord
{
	const ISingle = "single_check";
	const IMulti = "multi_check";
	const IText = "text";
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Question the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'question';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title', 'required'),
			array('is_mark, created_date, created_by', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>512),
			array('type', 'length', 'max'=>12),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, type, is_mark, created_date, created_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
			'type' => 'Type',
			'is_mark' => 'Is Mark',
			'created_date' => 'Created Date',
			'created_by' => 'Created By',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('is_mark',$this->is_mark);
		$criteria->compare('created_date',$this->created_date);
		$criteria->compare('created_by',$this->created_by);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getExam($limit = 0){
        $arr = array();
		$criteria = new CDbCriteria;
        $criteria->addCondition("type <> 'text'");
		$criteria->order = 'RAND()';
		if ($limit > 0)
			$criteria->limit = $limit;
		$data = $this->findAll($criteria);
        if ($data){
            foreach ($data as $item)
                $arr[] = $item;
        }

        $criteria = new CDbCriteria;
        $criteria->addCondition("type like 'text'");
        $criteria->order = 'RAND()';

        $arr[] = $this->find($criteria);
        return $arr;
	}
	
	public function getExamAnswer(){
		$criteria = new CDbCriteria;
		$criteria->order = 'is_not_range, RAND()';
		$criteria->addCondition('question_id = :question_id');
		$criteria->params = array('question_id' => $this->id);
		return Answer::model()->findAll($criteria);
	}
}